<template>
  <div class="publications-page">
    <Header />
    <main class="page-content">
      <div class="container">
        <h1 class="page-title">Publications</h1>
        
        <!-- 上部分：按年份分组的论文列表 -->
        <div class="publications-section">
          <div v-if="publicationsLoading" class="loading"></div>
          <div v-else-if="publicationsError" class="error">{{ publicationsError }}</div>
          <div v-else>
            <div v-for="yearGroup in publicationsData" :key="yearGroup.date" class="year-group">
              <h2 class="year-title">{{ yearGroup.date }}</h2>
              <div class="publications-list">
                <div v-for="(pub, index) in yearGroup.list" :key="pub.content_id" class="publication-item">
                  <div class="pub-number">{{ index + 1 }}.</div>
                  <div class="pub-content">
                    <p class="pub-authors">{{ pub.author }}</p>
                    <p class="pub-title">"{{ pub.title }}"</p>
                    <p class="pub-venue">
                      <em v-if="pub.journals">{{ pub.journals }}</em><span v-if="pub.impact_factor" class="pub-info"> ({{ pub.impact_factor }})</span>
                    </p>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 下部分：News 内容 -->
        <div class="news-section">
          <div v-if="newsLoading" class="loading"></div>
          <div v-else-if="newsError" class="error">{{ newsError }}</div>
          <div v-else>
            <div class="news-list">
              <article v-for="item in newsList" :key="item.content_id" class="news-item">
                <div class="news-content">
                  <div class="news-text">
                    <h2 
                      class="news-title-link"
                      @click="goToDetail('news', item.content_id)"
                    >
                      {{ item.title }}
                    </h2>
                    <p class="news-date">{{ formatDate(item.content_date) }}</p>
                    <p class="news-excerpt">{{ item.introduction }}</p>
                    <a 
                      :href="`/news/detail?type=news&id=${item.content_id}`" 
                      class="read-more"
                      @click.prevent="goToDetail('news', item.content_id)"
                    >
                      Read More »
                    </a>
                  </div>
                  <div 
                    v-if="item.image_url" 
                    class="news-image"
                    @click="goToDetail('news', item.content_id)"
                  >
                    <img :src="getImageUrl(item.image_url)" :alt="item.title">
                  </div>
                </div>
              </article>
            </div>

            <!-- News Pagination -->
            <div v-if="newsList.length > 0" class="pagination">
              <a 
                v-if="newsCurrentPage > 1"
                href="#" 
                class="pagination-link" 
                @click.prevent="prevNewsPage"
              >
                « Previous
              </a>
              <span v-else class="pagination-disabled">« Previous</span>
              
              <a 
                v-if="newsCurrentPage < newsTotalPages"
                href="#" 
                class="pagination-link" 
                @click.prevent="nextNewsPage"
              >
                Next »
              </a>
              <span v-else class="pagination-disabled">Next »</span>
            </div>
          </div>
        </div>
      </div>
    </main>
    <Footer />
  </div>
</template>

<script>
import Header from '../components/Header.vue'
import Footer from '../components/Footer.vue'
import { getPublicationGroupList, getNewsList, API_BASE_URL } from '@/utils/api'

export default {
  name: 'Publications',
  components: {
    Header,
    Footer
  },
  data() {
    return {
      // Publications数据
      publicationsData: null,
      publicationsLoading: false,
      publicationsError: null,
      
      // News数据
      newsList: [],
      newsCurrentPage: 1,
      newsPerPage: 8,
      newsTotal: 0,
      newsTotalPages: 1,
      newsLoading: false,
      newsError: null
    }
  },
  async mounted() {
    await Promise.all([
      this.fetchPublications(),
      this.fetchNewsList()
    ])
  },
  methods: {
    /**
     * 获取Publications分组列表
     */
    async fetchPublications() {
      this.publicationsLoading = true
      this.publicationsError = null
      try {
        this.publicationsData = await getPublicationGroupList()
      } catch (error) {
        console.error('获取Publications列表失败:', error)
        this.publicationsError = error.message || '获取出版物列表失败'
        this.publicationsData = null
      } finally {
        this.publicationsLoading = false
      }
    },
    
    /**
     * 获取News列表
     */
    async fetchNewsList() {
      this.newsLoading = true
      this.newsError = null
      try {
        const data = await getNewsList(this.newsCurrentPage, this.newsPerPage)
        this.newsList = data.list || []
        this.newsTotal = data.total || 0
        this.newsTotalPages = data.last_page || 1
        this.newsCurrentPage = data.current_page || 1
      } catch (error) {
        console.error('获取News列表失败:', error)
        this.newsError = error.message || '获取新闻列表失败'
        this.newsList = []
      } finally {
        this.newsLoading = false
      }
    },
    
    /**
     * News分页：上一页
     */
    async prevNewsPage() {
      if (this.newsCurrentPage > 1) {
        this.newsCurrentPage--
        await this.fetchNewsList()
        // 滚动到News区域
        const newsSection = document.querySelector('.news-section')
        if (newsSection) {
          newsSection.scrollIntoView({ behavior: 'smooth', block: 'start' })
        }
      }
    },
    
    /**
     * News分页：下一页
     */
    async nextNewsPage() {
      if (this.newsCurrentPage < this.newsTotalPages) {
        this.newsCurrentPage++
        await this.fetchNewsList()
        // 滚动到News区域
        const newsSection = document.querySelector('.news-section')
        if (newsSection) {
          newsSection.scrollIntoView({ behavior: 'smooth', block: 'start' })
        }
      }
    },
    
    /**
     * 格式化日期
     */
    formatDate(dateString) {
      if (!dateString) return ''
      const date = new Date(dateString)
      return date.toLocaleDateString('en-US', { 
        year: 'numeric', 
        month: 'long', 
        day: 'numeric' 
      })
    },
    
    /**
     * 获取图片完整URL
     */
    getImageUrl(imageUrl) {
      if (!imageUrl) return ''
      if (imageUrl.startsWith('http')) {
        return imageUrl
      }
      return `${API_BASE_URL}${imageUrl}`
    },
    
    /**
     * 跳转到详情页（新标签页打开）
     */
    goToDetail(type, contentId) {
      const url = `/news/detail?type=${type}&id=${contentId}`
      window.open(url, '_blank')
    }
  }
}
</script>

<style scoped>
/* Override container width for this page only */
.publications-page .container {
  max-width: 1200px;
}

.publications-page .page-title {
  text-align: center;
}

/* Publications Section */
.publications-section {
  margin-bottom: 80px;
}

.year-group {
  margin-bottom: 20px;
}

.year-title {
  font-size: 2.2em;
  color: var(--secondary-color);
  margin: 0 0 10px 0;
  padding-bottom: 5px;
  border-bottom: 2px solid var(--border-color);
  font-weight: 600;
}

.publications-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.publication-item {
  display: flex;
  gap: 15px;
  line-height: 1.8;
}

.pub-number {
  flex-shrink: 0;
  color: var(--secondary-color);
  font-weight: 500;
  font-size: 1em;
  min-width: 30px;
}

.pub-content {
  flex: 1;
}

.pub-authors {
  color: var(--text-color);
  margin: 0 0 5px 0;
  font-size: 0.95em;
}

.pub-authors::after {
  content: ',';
}

.pub-title {
  color: #c41e3a;
  font-style: italic;
  font-weight: bold;
  margin: 0 0 5px 0;
  font-size: 0.95em;
  line-height: 1.7;
}

.pub-venue {
  color: var(--text-color);
  margin: 0;
  font-size: 0.95em;
}

.pub-date {
  margin-left: 4px;
}

.pub-info {
  font-weight: 600;
  color: var(--secondary-color);
}

/* News Section */
.news-section {
  margin-top: 80px;
  padding-top: 40px;
  border-top: 2px solid var(--border-color);
}

.news-list {
  display: flex;
  flex-direction: column;
  gap: 50px;
  margin-bottom: 40px;
}

.news-item {
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 40px;
}

.news-item:last-child {
  border-bottom: none;
}

.news-content {
  display: flex;
  gap: 25px;
}

.news-text {
  flex: 1;
  min-width: 0;
}

.news-text h2 {
  color: #333;
  margin: 0 0 10px 0;
  font-size: 1.3em;
  line-height: 1.5;
  font-weight: 600;
}

.news-title-link {
  cursor: pointer;
  transition: color 0.3s ease;
}

.news-title-link:hover {
  color: var(--primary-color);
}

.news-date {
  color: #999;
  font-size: 0.9em;
  margin: 0 0 12px 0;
}

.news-excerpt {
  color: var(--text-color);
  line-height: 1.8;
  margin: 0 0 15px 0;
  font-size: 0.95em;
}

.news-image {
  flex-shrink: 0;
  width: 200px;
  display: inline-block;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: opacity 0.3s ease;
}

.news-image:hover {
  opacity: 0.8;
}

.news-image img {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 8px;
  transition: transform 0.3s ease;
}

.news-image:hover img {
  transform: scale(1.05);
}

.read-more {
  color: var(--primary-color);
  font-weight: 500;
  transition: color 0.3s ease;
  display: inline-block;
  font-size: 0.9em;
}

.read-more:hover {
  color: var(--hover-color);
  text-decoration: underline;
}

/* Pagination Styles */
.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 50px;
  padding-top: 30px;
  border-top: 1px solid var(--border-color);
}

.pagination-link {
  color: var(--primary-color);
  font-weight: 500;
  font-size: 0.95em;
  text-decoration: none;
  transition: all 0.3s ease;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 3px;
}

.pagination-link:hover {
  color: white;
  background-color: var(--primary-color);
  text-decoration: none;
}

.pagination-disabled {
  color: #ccc;
  font-size: 0.95em;
  cursor: default;
  padding: 5px 10px;
}

.pagination-info {
  color: #666;
  font-size: 0.9em;
  padding: 5px 15px;
}

.loading {
  text-align: center;
  padding: 40px 0;
  color: #666;
  font-size: 1em;
}

.error {
  text-align: center;
  padding: 40px 0;
  color: #e74c3c;
  font-size: 1em;
}

/* Responsive Design */
@media (max-width: 768px) {
  .year-title {
    font-size: 1.8em;
  }
  
  .publication-item {
    flex-direction: column;
    gap: 5px;
  }
  
  .pub-number {
    min-width: auto;
  }
  
  .news-content {
    flex-direction: column;
  }
  
  .news-image {
    width: 100%;
    height: 220px;
  }
  
  .pagination {
    flex-wrap: wrap;
    gap: 15px;
  }
}
</style>

